<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $user_id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $date
 * @property string $role
 */
class User extends CActiveRecord
{
	 public $password1;
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		 return array(
                        array('username, email, password, password1,role','required'),
                        
                        // magic is out there
            array('email',    'match',   'pattern'    => '/^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([a-z\.]{2,6})$/', 'message' => 'неверный формат e-mail'),
            array('username', 'match',   'pattern'    => '/^[A-Za-z0-9_-�-��-�\s,]+$/u','message'  => 'недопустимый login'),
            array('password1', 'compare', 'compareAttribute' => 'password'),
            
                        array('username, email', 'length', 'max'=>100),
                       // array('activationKey', 'length', 'max'=>32),
                        array('password, password1', 'length',  'max' => 40,  'min' => 5,),
                        // The following rule is used by search().
                        // Please remove those attributes that should not be searched.
                        array('user_id, username, email, password, createtime, activationKey, role', 'safe', 'on'=>'search'),
                );
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		  'Coments'=>array(self::HAS_MANY,'Coments','id_user'),  
			'Profile'=>array(self::HAS_ONE,'Profile','user_id'),
			'Usercv'=>array(self::HAS_MANY,'Usercv','user_id'),
			'Job'=>array(self::HAS_MANY,'Job','user_id'),
			'News'=>array(self::HAS_MANY,'News','user_id'),
			'Post'=>array(self::HAS_MANY,'Post','user_id'),
		);
	}

		public function recently($offset,$limit)
		{
		    $this->getDbCriteria()->mergeWith(array(
		        'order'=>'username ASC',
		        'limit'=>$limit,
		        'offset'=>$offset,
		        
		    ));
	    return $this;
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'date' => 'Date',
			'role' => 'Role',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('role',$this->role,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}